package de.dhbw.tinf11b.vfb.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.Date;

import org.junit.Test;

import de.dhbw.tinf11b.vfb.BachelorsDegree;
import de.dhbw.tinf11b.vfb.Course;
import de.dhbw.tinf11b.vfb.CourseManager;
import de.dhbw.tinf11b.vfb.Curriculum;
import de.dhbw.tinf11b.vfb.CurriculumManager;
import de.dhbw.tinf11b.vfb.Student;
import de.dhbw.tinf11b.vfb.StudentManager;
import de.dhbw.tinf11b.vfb.exceptions.CourseNotFoundException;
import de.dhbw.tinf11b.vfb.exceptions.CurriculumNotFoundException;
import de.dhbw.tinf11b.vfb.exceptions.MarkOutOfBoundsException;
import de.dhbw.tinf11b.vfb.exceptions.StudentNotFoundException;
import de.dhbw.tinf11b.vfb.exceptions.StudyNotCompleteException;
import de.dhbw.tinf11b.vfb.exceptions.UnitNotFoundException;
import de.dhbw.tinf11b.vfb.exceptions.WeightingsMismatchException;

public class MarkTest {

	@Test
	public void testMarks() throws WeightingsMismatchException, CurriculumNotFoundException, StudentNotFoundException, CourseNotFoundException, MarkOutOfBoundsException, UnitNotFoundException, StudyNotCompleteException {
		
		CurriculumManagerTest curriculumManagerTest = new CurriculumManagerTest();
		curriculumManagerTest.testCurriculum();
		
		Curriculum curriculum = CurriculumManager.getInstance().getCurriculum("Angewandte Informatik Stand 2011");
		CourseManager.getInstance().getCourses().add(new Course("TINF11B", 2011, "5.01", curriculum, "Schwinnie"));
		
		Course course = CourseManager.getInstance().getCourseByName("TINF11B");
		assertEquals(course.getCourseDirector(), "Schwinnie");
		assertEquals(course.getCurriculum(), curriculum);
		assertEquals(course.getRoom(), "5.01");
		assertTrue(course.getYear() == 2011);
		
		
		// add student
		StudentManagerTest studentManagerTest = new StudentManagerTest();
		studentManagerTest.testAddStudent();
		
		Student student = StudentManager.getInstance().getStudentByNameAndBirthDate("Thomas", "Klytta", new Date(1988, 3, 4));
		student.immatriculate(course);
	
		try {
			student.issueBachelorsDegree();
		} catch (Exception e) {
			assertTrue(e instanceof StudyNotCompleteException);
		}
		
		// add marks
		student.addResultMark(curriculum.getUnitByName("Lineare Algebra"), (float) 5.0);
		student.addResultMark(curriculum.getUnitByName("Analysis"), (float) 1.0);
		
		// at this point, the study is not completed, mark for "Grundlagen und Logik" is missing
		try {
			student.issueBachelorsDegree();
		} catch (Exception e) {
			assertTrue(e instanceof StudyNotCompleteException);
		}
		
		// add the missing mark
		student.addResultMark(curriculum.getUnitByName("Grundlagen und Logik"), (float) 2.8);
		
		student.setBachelorThesisMark((float) 2.0);
		BachelorsDegree degree = student.issueBachelorsDegree();
		
		// the bachelor mark with this data is 2.7384615
		assertTrue(degree.getMark() > (float) 2.73 && degree.getMark() < (float) 2.74);
		
	}

}
